package cn.wolfcode.rbac.util;

import cn.wolfcode.rbac.domain.Employee;
import cn.wolfcode.rbac.domain.Permission;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpSession;
import java.util.List;

public abstract class UserContext {
    //设置常量，用于存储登录用户的信息
    public static  final String EMP_IN_SESSION = "EMP_IN_SESSION";
    //设置常量，用于存储登录用户的权限的
    public static final  String EXPRESSION_IN_SESSION="EXPRESSION_IN_SESSION";
    /*
        将登录用户的信息存储到"EMP_IN_SESSION"中，当前端取数据时，调用常量EMP_IN_SESSION就是相当于调用了登录的对象，就可以直接获取到信息
     */
    public static void setCurrentUser(Employee emp){
        getSession().setAttribute("EMP_IN_SESSION",emp);
    }
    //创建get方法用于前端取数据用的
    public static Employee getCurrentUser(){
        return (Employee)getSession().getAttribute("EMP_IN_SESSION");
    }
    //由于，servlet被controller替换，因此使用此方法来获取到session对象，
    public static HttpSession getSession(){
        ServletRequestAttributes attrs = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        return attrs.getRequest().getSession();
    }

    //将权限保存到session中
    public static void setExpression(List<String> permissions){
        getSession().setAttribute("EXPRESSION_IN_SESSION",permissions);
    }
    public static List<String> getExpression(){
        return (List) getSession().getAttribute("EXPRESSION_IN_SESSION");
    }

}
